﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi.PostModel;
using WebApi.Models;
using WebApi.Class;
using Yd.Domain.DAL;
using Yd.BLL;
using System.Data;
using System.Data.SqlClient;
using Yd.Model;
using System.Text;
namespace WebApi.Controllers
{
    public class ShopsController : ApiController
    {
        public CommDAL commdal = new CommDAL();
        // Post 用户登录 api/user/login
        /// <returns>登录成功返回userinfo  -1异常 -4 参数错误</returns>
        public object ShopLogin([FromBody]PostLogin post)
        {
            ModelBase mbase = new ModelBase();
            if (StringUtils.getIsEmpty(post.code))
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }
            if (commdal.g2byopenid(post.openid))
            {
                mbase.code = -1;
                mbase.message = "您已被管理员设置为黑名单!";
                return mbase;
            }
            WechatLoginInfo login = new WechatLoginInfo();
            login.code = post.code;
            WeChatAppDecrypt chatapp = new WeChatAppDecrypt("wxd219d8ad073adeaa", "9de5bdb840330ef17f335ba179254cc7");
            OpenIdAndSessionKey openid = chatapp.DecodeOpenIdAndSessionKey(login);
            if (openid == null)
            {
                mbase.code = -8;
                mbase.message = ErrorCodeUtil.getCodeMessage(-8);
                return mbase;
            }
            yd_shop tzuser = new yd_shop();
            tzuser.openid = openid.openid;
            tzuser.session_key = openid.session_key;
            int ret = commdal.InsertShop(tzuser);
            if (ret < 0)
            {
                mbase.code = -1;
                mbase.message = ErrorCodeUtil.getCodeMessage(-1);
                return mbase;
            }
            SqlParameter[] fparam = new SqlParameter[] { 
                new SqlParameter("@openid",openid.openid),
                new SqlParameter("@suid",ret),
            };
            DataSet ds = commdal.QueryObject("getShopInfoByOpenId", fparam);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                ShopInfoModel userInfo = ShopInfoModel.GetUserInfo(ds.Tables[0].Rows[0],true);
                if (userInfo != null)
                {
                    mbase.code = 0;
                    userInfo.isFristLogin = ret != 0 ? 1 : 0;//用户编号为0 不是第一次登陆
                    mbase.data = userInfo;
                    mbase.message = ErrorCodeUtil.getCodeMessage(0);
                }
                else
                {
                    mbase.code = -1;
                    mbase.message = ErrorCodeUtil.getCodeMessage(-1);
                }
            }
            else
            {
                mbase.code = -1;
                mbase.message = ErrorCodeUtil.getCodeMessage(-1); ;
            }
            return mbase;
        }

        // Post 獲取用戶信息 api/users/PostUserInfo
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object PostShopInfo([FromBody]PostShopInfoById post)
        {
            ModelBase mbase = new ModelBase();
            if (post.shopId == 0)
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }
            if (commdal.g2(post.shopId))
            {
                mbase.code = -1;
                mbase.message = "您已被管理员设置为黑名单!";
                return mbase;
            }
            SqlParameter[] fparam = new SqlParameter[] { 
                new SqlParameter("@openid",@""),
                new SqlParameter("@suid",post.shopId)
            };
            DataSet dsuser = commdal.QueryObject("getShopInfoByOpenId", fparam);
            if (dsuser != null && dsuser.Tables[0].Rows.Count > 0)
            {
                ShopInfoModel userInfo = ShopInfoModel.GetUserInfo(dsuser.Tables[0].Rows[0],false);
                if (userInfo != null)
                {
                    mbase.code = 0;
                    mbase.data = userInfo;
                    mbase.message = ErrorCodeUtil.getCodeMessage(0);
                }
                else
                {
                    mbase.code = -1;
                    mbase.message = ErrorCodeUtil.getCodeMessage(-1);
                }
            }
            else
            {
                mbase.code = -3;
                mbase.message = ErrorCodeUtil.getCodeMessage(-3);
            }
            return mbase;
        }

        // Post 獲取用戶信息 api/users/PostEditshop
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object PostEditShop([FromBody]PostEditShop post)
        {
            ModelBase mbase = new ModelBase();
            if (post.shopId == 0 || (StringUtils.getIsEmpty(post.shopName) && StringUtils.getIsEmpty(post.contactTel) && StringUtils.getIsEmpty(post.address) && StringUtils.getIsEmpty(post.shopDetails) && StringUtils.getIsEmpty(post.shopAdvImg) && StringUtils.getIsEmpty(post.shopSummary) && StringUtils.getIsEmpty(post.shopStartTime) && StringUtils.getIsEmpty(post.shopEndTime) && StringUtils.getIsEmpty(post.shopFrontCover)))
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }
            if (commdal.g2(post.shopId))
            {
                mbase.code = -1;
                mbase.message = "您已被管理员设置为黑名单!";
                return mbase;
            }
            StringBuilder sql = new StringBuilder();
            sql.Append("update yd_shop set ");
            if (!StringUtils.getIsEmpty(post.shopName))
            {
                sql.Append(" shopName='" + post.shopName + "',");
            }
            if (!StringUtils.getIsEmpty(post.contactTel))
            {
                sql.Append(" contactTel='" + post.contactTel + "',");
            }
            if (!StringUtils.getIsEmpty(post.address))
            {
                sql.Append(" address='" + post.address + "',");
            }
            if (post.longitude > 0)
            {
                sql.Append(" longitude=" + post.longitude+",");
            }
            if (post.latitude > 0)
            {
                sql.Append(" latitude=" + post.latitude+",");
            }
            if (!StringUtils.getIsEmpty(post.shopDetails))
            {
                sql.Append(" shopDetails='" + post.shopDetails + "',");
            }
            if (!StringUtils.getIsEmpty(post.shopAdvImg))
            {
                sql.Append(" shopAdvImg='" + post.shopAdvImg + "',");
            }
            if (!StringUtils.getIsEmpty(post.shopSummary))
            {
                sql.Append(" shopSummary='" + post.shopSummary + "',");
            }
            if (!StringUtils.getIsEmpty(post.shopStartTime))
            {
                sql.Append(" shopStartTime='" + post.shopStartTime + "',");
            }
            if (!StringUtils.getIsEmpty(post.shopEndTime))
            {
                sql.Append(" shopEndTime='" + post.shopEndTime + "',");
            }
            if (!StringUtils.getIsEmpty(post.shopFrontCover))
            {
                sql.Append(" shopFrontCover='" + post.shopFrontCover + "',");
            }
            sql.Remove(sql.Length - 1, 1);
            sql.Append(" where suid=" + post.shopId);
            int _fresult = -1;
            _fresult = commdal.SaveOrUpdateSql(sql.ToString());

            if (_fresult == 0)
            {
                mbase.code = 0;
                mbase.message = "修改成功";
                return mbase;
            }
            else
            {
                mbase.code = -1;
                mbase.message = ErrorCodeUtil.getCodeMessage(-1);
            }
            return mbase;
        }

        /// Post 獲取商家列表 api/user/shopList
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object shopList([FromBody]PostShopsList post)
        {
            ModelBase mbase = new ModelBase();
            string pagewhere = " isnull(isForbidden,0)=0 ";
            if (post.pageSize == 0) {
                post.pageSize = 10;
            }
            DataSet newlist = commdal.QueryObjectAllSql("declare @t int Execute Proce_Sql_PageChange 'view_shoplist','  * ','suid desc','" + pagewhere + "'," + post.pageSize + "," + post.pageIndex + ",@t output");
            if (newlist != null)
            {
                mbase.code = 0;
                mbase.data = newlist.Tables[0];
                mbase.message = "获取成功";
            }
            else
            {
                mbase.code = -1;
                mbase.message = ErrorCodeUtil.getCodeMessage(-1);
            }
            return mbase;
        }

        /// Post 添加通知formid api/kazuo/shopList
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object InsertFormId([FromBody]PostAddFromId post)
        {
            ModelBase mbase = new ModelBase();
            if (post.shopId == 0 || StringUtils.getIsEmpty(post.wxFormid))
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }

            SqlParameter[] fparam = new SqlParameter[] { 
                new SqlParameter("@shopid",post.shopId),
                new SqlParameter("@wxFormid",post.wxFormid)
            };
            int _fresult = -1;
            _fresult = commdal.UpdateDataByPro("InsertFormId", fparam);
            if (_fresult == 0)
            {
                mbase.code = 0;
                mbase.message = "添加成功";
                return mbase;
            }
            else
            {
                mbase.code = _fresult;
                mbase.message = ErrorCodeUtil.getCodeMessage(_fresult);
            }
            return mbase;
        }

        /// Post 獲取列表 api/user/shopList
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object hotShopList([FromBody]PostShopsList post)
        {
            ModelBase mbase = new ModelBase();
            string pagewhere = " isRemen=1 ";
            if (post.pageSize == 0)
            {
                post.pageSize = 10;
            }
            DataSet newlist = commdal.QueryObjectAllSql("declare @t int Execute Proce_Sql_PageChange 'view_shoplist',' * ','sequence asc','" + pagewhere + "',5,1,@t output");
            if (newlist != null)
            {
                mbase.code = 0;
                mbase.data = newlist.Tables[0];
                mbase.message = "获取成功";
            }
            else
            {
                mbase.code = -1;
                mbase.message = ErrorCodeUtil.getCodeMessage(-1);
            }
            return mbase;
        }

        // Post 用户端獲取商户详情 api/users/PostUserInfo
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object ShopDetails([FromBody]PostShopInfoById post)
        {
            ModelBase mbase = new ModelBase();
            if (post.shopId == 0 || post.userId == 0)
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }
            SqlParameter[] fparam = new SqlParameter[] { 
                new SqlParameter("@openid",@""),
                new SqlParameter("@suid",post.shopId)
            };
            DataSet dsuser = commdal.QueryObject("getShopInfoByOpenId", fparam);
            if (dsuser != null && dsuser.Tables[0].Rows.Count > 0)
            {
                ShopInfoModel userInfo = ShopInfoModel.GetUserInfo(dsuser.Tables[0].Rows[0], false);
                if (userInfo != null)
                {
                    //浏览记录保存
                    SqlParameter[] liulanjilv = new SqlParameter[] { 
                new SqlParameter("@shopid",post.shopId),
                new SqlParameter("@uid",post.userId)
            };
                    int _fresult = -1;
                    _fresult = commdal.UpdateDataByPro("InsertShopHistory", liulanjilv);
                    DataSet ds1 = commdal.QueryObjectAllSql("SELECT fid FROM [dbo].[yd_favorites] where shopid=" + post.shopId + " and uid=" + post.userId);
                    if (ds1 != null && ds1.Tables[0].Rows.Count > 0) {
                        userInfo.isFavorites = 1;
                    }
                    mbase.code = 0;
                    mbase.data = userInfo;
                    mbase.message = ErrorCodeUtil.getCodeMessage(0);
                }
                else
                {
                    mbase.code = -1;
                    mbase.message = ErrorCodeUtil.getCodeMessage(-1);
                }
            }
            else
            {
                mbase.code = -3;
                mbase.message = ErrorCodeUtil.getCodeMessage(-3);
            }
            return mbase;
        }
    }
}
